<?php

include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/header.inc');
include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/comp.inc');
include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/fcs.php');
?>


<?php

if(isset($_GET['task']))
{
		if(!$link=mysql_connect("localhost",$user,$pass))
			die("Error in Database Connection");

		if(!mysql_select_db("bs"))
			echo("Error in Database Selection");

		if($_GET['task']=="display")
		{
				$valid=1;
				
				echo "	| <a href='trial_balance.php?task=displayform'>BS Summary</a></div> ";
				
				//IS THERE NOT A LEDGER ID
				
				if(!isset($_GET['ledger_id']))
				{
						echo "It is not a Valid Ledger ID";
						$valid=0;
				}
				else
				{
						$result=mysql_query("select ldname,under from bs_ledgers where id='{$_GET['ledger_id']}'");
						if(!$result)
								echo "Error in fetching ledger name";
						else 
						{
								$arr=mysql_fetch_array($result);
								$ldname=$arr['ldname'];
								$under=$arr['under'];
								echo " <h3> {$ldname} :  From <font color='#FF6600'>{$_GET['fromdate']}</font> To <font color='#FF6600'>{$_GET['todate']}</font></h3>";
						}
				}
				
		

				
				//VALIDATING DATEs
				
				if(!ereg("^[0-3]?[0-9][\./\-][0-1]?[0-9][\-]20[0-9][0-9]$",$_GET['fromdate'])) 
				{
						echo "Check Your Input 'From' Date<br>";
						$valid=0;
				}
				else
				{
					$datearr=explode("-",$_GET['fromdate']);
					if(!checkdate($datearr[1],$datearr[0],$datearr[2]))
					{
							echo "Check Your Input 'From' Date<br>";
							$valid=0;
					}
					else
							$fromdate=date("Y-m-d",mktime(0,0,0,$datearr[1],$datearr[0],$datearr[2]));
				}
				
				
				if(!ereg("^[0-3]?[0-9][\./\-][0-1]?[0-9][\-]20[0-9][0-9]$",$_GET['todate'])) 
				{
						echo "Check Your Input 'To' Date<br>";
						$valid=0;
				}
				else
				{
					$datearr=explode("-",$_GET['todate']);
					if(!checkdate($datearr[1],$datearr[0],$datearr[2]))
					{
							echo "Check Your Input 'To' Date<br>";
							$valid=0;
					}
					else
							$todate=date("Y-m-d",mktime(0,0,0,$datearr[1],$datearr[0],$datearr[2]));
				}
				

				if($valid==1)
				{
								echo "<div class='main'><a href='trial_balance.php?fromdate={$_GET['fromdate']}&todate={$_GET['todate']}&task=display&group_id={$under}'><--</a></div>";
								
								$tablename="bs_ledger_".ereg_replace(" ","_",$ldname);
								if(isset($_GET['with_date']))
								{
									$result=mysql_query("select * from {$tablename} where date>='{$fromdate}' and date<='{$todate}' order by date ");//.$condition.$ledger.$pledger.$search );
								}
								else
									$result=mysql_query("select * from {$tablename} where date>='{$fromdate}' and date<='{$todate}' ");//.$condition.$ledger.$pledger.$search );
									
								if(!$result)
										echo "Ledger Extraction Err: ".mysql_error();
								else
								{
										
										echo "<table border='1' class='main' cellpadding='3' >";
										echo "<tr><th>Id</th><th><a href='".$_SERVER['PHP_SELF']."?fromdate={$_GET['fromdate']}&todate={$_GET['todate']}&task=display&ledger_id={$_GET['ledger_id']}&with_date=yes'>Date</a></th><th>Voucher Type</th>";if(!isset($_GET['with_date']))echo"<th>Voucher ID</th>";echo"<th>Debit</th><th>Credit</th><th>Description</th><th> By/To Ledger</th><tr>";										
										$row=0;
										
										//SHOW OPENING BALANCE
										
										//WHAT IS THE NATURE OF LEDGER
										$traced=0;
										while($traced==0)
										{
												$result2=mysql_query("select * from bs_groups where id='{$under}'");
												if(!$result2)
														echo "error";
												else
												{
														$arr2=mysql_fetch_array($result2);
														if($arr2['under']=='00')
														{
																$traced=1;
																break;
														}
														else
														{
																$under=$arr2['under'];
														}
						
												}
										}
								
										$nature=$arr2['nature'];
							
										
										//END
										
										if($nature!="EXP" and $nature!="INC")
										{
												$opb_r=mysql_query("select (SUM(debit)-SUM(credit)) as opb from {$tablename} where date<'{$fromdate}' ");										
												$arr_opb=mysql_fetch_array($opb_r);
												$opb=(int)$arr_opb['opb'];
												
										}
										else
												$opb=0;
												
										if($opb!=0)
										{
												if($opb>0)
												{
														echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td>  <td>Period Opening Balance</td>";if(!isset($_GET['with_date']))echo"<td></td>";echo" <td><div align='right'><b>{$opb}</b></div></td>  <td></td>  <td></td>  <td></td></tr>";
												}
												else
												{
														$opb=abs($opb);			
														echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td>  <td>Period Opening Balance</td>";if(!isset($_GET['with_date']))echo"  <td></td>";echo" <td></td>  <td><div align='right'><b>{$opb}</b></div></td>   <td></td>  <td></td></tr>";
														
												
												}
												

										
												$row++;
										}
										
										//END
										
										$count_print_display = 0; // for with date
										while($arr=mysql_fetch_array($result))
										{

												if($arr['voucher_type']=="OPB")
												{
														$link="";
														$bill_no="";
														$description="Provided Opening Balance";
														$by_to_ledger="";
												}
												if($arr['voucher_type']=="PURCHASE")
												{
														$link="<a href=pbook_trans.php?task=edit_form&pid=".$arr['voucher_id'].">".$arr['voucher_id']."</a.";

														$r=mysql_query("select *,l1.ldname as leftside,l2.ldname as rightside from bs_purchase_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.id={$arr['voucher_id']} and p.ledger_id=l1.id and  p.purchase_ledger_id=l2.id");
														if(!$r)
															echo "Error";
														$a=mysql_fetch_array($r);
														$bill_no=$a['bill_no'];
														$description=$a['description']."<b>:Bill No:</b> {$bill_no}";
														
														if($a['purchase_ledger_id']==$_GET['ledger_id'])
																$by_to_ledger=$a['leftside'];
														else
																$by_to_ledger=$a['rightside'];
				

				

												}
												if($arr['voucher_type']=="SALES")
												{
														$link="<a href=sbook_trans.php?task=edit_form&sid=".$arr['voucher_id'].">".$arr['voucher_id']."</a.";
														$r=mysql_query("select *,l1.ldname as leftside,l2.ldname as rightside from bs_sales_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.id={$arr['voucher_id']} and p.ledger_id=l1.id and  p.sales_ledger_id=l2.id");
														if(!$r)
															echo "Error";
														$a=mysql_fetch_array($r);
														$bill_no=$a['bill_no'];
														$description=$a['description']."<b>:Bill No:</b> {$bill_no}";
														
														if($a['sales_ledger_id']==$_GET['ledger_id'])
																$by_to_ledger=$a['leftside'];
														else
																$by_to_ledger=$a['rightside'];
				

												
												
												}
												if($arr['voucher_type']=="PAYMENT")
												{
														$link="<a href=payment_trans.php?task=edit_form&pid=".$arr['voucher_id'].">".$arr['voucher_id']."</a.";
														$r=mysql_query("select *,l1.ldname as leftside,l2.ldname as rightside from bs_payment_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.id={$arr['voucher_id']} and p.ledger_id=l1.id and  p.source_ledger_id=l2.id");
														if(!$r)
															echo "Error";
														$a=mysql_fetch_array($r);
														$description=$a['description'];
														
														if($a['source_ledger_id']==$_GET['ledger_id'])
																$by_to_ledger=$a['leftside'];
														else
																$by_to_ledger=$a['rightside'];
				

												}
												if($arr['voucher_type']=="RECEIPT")
												{
														$link="<a href=receipt_trans.php?task=edit_form&pid=".$arr['voucher_id'].">".$arr['voucher_id']."</a.";
														$r=mysql_query("select *,l1.ldname as leftside,l2.ldname as rightside from bs_receipt_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.id={$arr['voucher_id']} and p.ledger_id=l1.id and  p.source_ledger_id=l2.id");
														if(!$r)
															echo "Error";
														$a=mysql_fetch_array($r);
														$description=$a['description'];
														
														if($a['source_ledger_id']==$_GET['ledger_id'])
																$by_to_ledger=$a['leftside'];
														else
																$by_to_ledger=$a['rightside'];
				
												
												}
												if($arr['voucher_type']=="JOURNAL" )
												{
														$link="<a href=journal_trans.php?task=edit_form&pid=".$arr['voucher_id'].">".$arr['voucher_id']."</a.";
														$r=mysql_query("select *,l1.ldname as leftside,l2.ldname as rightside from bs_journal_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.id={$arr['voucher_id']} and p.debit_ledger_id=l1.id and  p.credit_ledger_id=l2.id");
														if(!$r)
															echo "Error";
														$a=mysql_fetch_array($r);
														$description=$a['description'];
														
														if($a['credit_ledger_id']==$_GET['ledger_id'])
																$by_to_ledger=$a['leftside'];
														else
																$by_to_ledger=$a['rightside'];
												
												}
												if($arr['voucher_type']=="CONTRA" )
												{
														$link="<a href=contra_trans.php?task=edit_form&pid=".$arr['voucher_id'].">".$arr['voucher_id']."</a.";
														$r=mysql_query("select *,l1.ldname as leftside,l2.ldname as rightside from bs_contra_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.id={$arr['voucher_id']} and p.debit_ledger_id=l1.id and  p.credit_ledger_id=l2.id");
														if(!$r)
															echo "Error";
														$a=mysql_fetch_array($r);
														$description=$a['description'];
														
														if($a['credit_ledger_id']==$_GET['ledger_id'])
																$by_to_ledger=$a['leftside'];
														else
																$by_to_ledger=$a['rightside'];
												
												}
												
												
												
					
												//DATE CONVERSION TO DD-MM-YYYY
												$arrdate=explode("-",$arr['date']);
												$date=date("d-m-Y",mktime(0,0,0,$arrdate[1],$arrdate[2],$arrdate[0]));								
												$count_print_display++;
												
												//DISPLAYING
												
												echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td>"; if(isset($_GET['with_date'])) echo "{$count_print_display}"; else echo"{$arr['id']}";echo"</td><td>{$date}</td><td>{$arr['voucher_type']}</td>";if(!isset($_GET['with_date']))echo"<td>{$link}</td>";echo"<td>";if($arr['debit']!=0) echo"<div align='right'><b>{$arr['debit']}</b></div>";echo"</td><td>";if($arr['credit']!=0) echo"<div align='right'><b>{$arr['credit']}</b></div>";echo"</td><td>{$description}</td><td>{$by_to_ledger}</td></tr>";
				
												$row++;
										}

										//EXTRACTING SUM
											//debit
												$result_for_debit_side=mysql_query("select SUM(debit) as sum_debit from {$tablename} where date>='{$fromdate}' and date<='{$todate}' ");

										if(!$result_for_debit_side)
												echo "Extraction Err: ".mysql_error();
										else
												$arr_for_debit_side=mysql_fetch_array($result_for_debit_side);
										
										$sum_for_debit_side=(int)$arr_for_debit_side['sum_debit'];

										if($sum_for_debit_side == 0)
											$sum_for_debit_side = '';

										
											//credit
											$result_for_credit_side=mysql_query("select SUM(credit) as sum_credit from {$tablename} where date>='{$fromdate}' and date<='{$todate}' ");

										if(!$result_for_credit_side)
												echo "Extraction Err: ".mysql_error();
										else
												$arr_for_credit_side=mysql_fetch_array($result_for_credit_side);
										
										$sum_for_credit_side=(int)$arr_for_credit_side['sum_credit'];
										
										if($sum_for_credit_side == 0)
											$sum_for_credit_side = '';
											
										if($sum_for_debit_side>0 || $sum_for_credit_side>0)
										{
												echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td>  <td><b>Current Total</b></td>  ";if(!isset($_GET['with_date']))echo"<td></td>";echo"   <td><div align='right'><b>{$sum_for_debit_side}</b></div></td>   <td><div align='right'><b>{$sum_for_credit_side}</b></div></td> <td></td><td></td>  </tr>";												
												
												$row++;
										}
										
										//EXTRACTING BALANCE
										if($nature!="EXP" and $nature!="INC")
										{
												$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$tablename} where date<='{$todate}' ");//FOR EXP AND INC where date>='{$fromdate}' and date<='{$todate}'  ");//.$condition.$ledger.$pledger.$search); PATCH
										}
										else
										{
												$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$tablename} where date>='{$fromdate}' and date<='{$todate}'  ");//.$condition.$ledger.$pledger.$search);

										}
										if(!$result1)
												echo "Extraction Err: ".mysql_error();
										else
												$arr1=mysql_fetch_array($result1);
										
										$balance=(int)$arr1['balance'];
										
										if($balance>0)
										{
												echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td>  <td><b>Balance</b></td>  ";if(!isset($_GET['with_date']))echo"<td></td>";echo"   <td><div align='right'><b>{$balance}</b></div></td>   <td>Rs</td> <td></td><td></td>  </tr>";												
										}
										else if($balance<0)
										{
												$balance=abs($balance);
												echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td>  <td><b>Balance</b></td>";if(!isset($_GET['with_date']))echo"  <td></td>";echo"    <td></td>   <td><div align='right'><b>{$balance}</b></div></td> <td>Rs</td><td></td> </tr>";												

										}
										else
										{
												echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td>  <td><b>Balance</b></td>";if(!isset($_GET['with_date']))echo"  <td></td>";echo"    <td><div align='right'><b>0</b></div></td>   <td><div align='right'><b>0</b></div></td><td></td><td></td> </tr>";												

										
										
										}
												
												
										
										echo "</table>";
								}
								
						}//END OF WITHOUT DETAIL

				
								

		
		
		}//END OF TASK=DISPLAY
}//END OF ISSET GET


?>
